{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.2 64-bit",
   "metadata": {
    "interpreter": {
     "hash": "1b69bd087aa8abd614583c53268aa6b11c2c1c046d6f6b67ad1f1c711f8a952b"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import sklearn\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "D_FILE_PATH = \"d_train.csv\"\n",
    "SYS_ENCODING = \"ANSI\"\n",
    "df = pd.read_csv(D_FILE_PATH)\n",
    "del df['体检日期']\n",
    "sex_map = {'男':1,'女':-1}\n",
    "# df['性别'] = df['性别'].astype(sex_map)\n",
    "df['性别'] = df['性别'].map(sex_map)\n",
    "# for i in range(0,41):\n",
    "#     print(df.columns[i])\n",
    "# df.columns\n",
    "# df[0]\n",
    "# df.dtypes\n",
    "# df['乙肝表面抗原']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "before_mean = df.mean(axis = 0, skipna = True)\n",
    "fill_dict = {}\n",
    "for i in range(0,40):\n",
    "    fill_dict[df.columns[i]] = before_mean[i]\n",
    "# print(fill_dict)\n",
    "df.fillna(fill_dict,inplace = True)\n",
    "df.to_csv('after_wash.csv',encoding = 'ANSI',index = 0)\n",
    "# df['乙肝表面抗原']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('after_wash.csv',encoding = SYS_ENCODING)\n",
    "df = df.iloc[:,1:]\n",
    "cols = list(df.columns.values)\n",
    "print(cols)\n",
    "cols.remove('血糖')\n",
    "# cols.remove('id')\n",
    "X = df[cols]\n",
    "Y = df['血糖']\n",
    "X_train = X[:5000]\n",
    "X_test = X[5000:len(X)]\n",
    "Y_train = Y[:5000]\n",
    "Y_test = Y[5000:len(Y)]\n",
    "Y_test[5012]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "from sklearn.linear_model import LinearRegression\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "model = LinearRegression()\n",
    "model.fit(X_train,Y_train)\n",
    "Y_predict = model.predict(X_test)\n",
    "for i in range(len(Y_predict)):\n",
    "    Y_predict[i] = round(Y_predict[i],3)\n",
    "print(Y_predict)\n",
    "# print(Y_predict)\n",
    "score = 0\n",
    "for i in range(642):\n",
    "    score = score + (Y_predict[i] - Y[i+5000]) * (Y_predict[i] - Y[i+5000])\n",
    "score = score / (642 * 2)\n",
    "print(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}